package com.yunche.yunchebusiness.mvp.presenter

import android.annotation.SuppressLint
import com.alibaba.fastjson.JSONObject
import com.yunche.yunchebusiness.base.mvp.BasePresenter
import com.yunche.yunchebusiness.mvp.contract.LoginContract
import com.yunche.yunchebusiness.mvp.model.LoginModel
import com.yunche.yunchebusiness.network.RequestJson
import com.yunche.yunchebusiness.network.exception.ErrorStatus
import com.yunche.yunchebusiness.network.exception.ExceptionHandle

/**
 * Create by Crcker on 2018/12/25 10:33
 * Email: Crcker@163.com
 */
@SuppressLint("CheckResult")


class LoginPresenter : BasePresenter<LoginContract.View>(), LoginContract.Presenter {
    private val model: LoginModel by lazy {
        LoginModel()
    }

    override fun login(phone: String, password: String) {
        checkViewAttached()
        var para = JSONObject()
        para["mobile"] = phone
        para["password"] = password
        mRootView?.showLoading()
        var disposable =  model.login(RequestJson.getRequestBody(para.toJSONString()))
            .subscribe({ it ->
                mRootView!!.apply {
                    hideLoading()
                    if (it.resultCode == ErrorStatus.SUCCESS) {
                        loginSuccess(it.datas!!)
                    } else {
                        showError(it.message!!)
                    }
                }
            }, {
                mRootView?.hideLoading()
                mRootView?.showError(ExceptionHandle.handleException(it))
            })

        addSubscription(disposable)

    }
}